package com.uca.sisbec.actions;

import com.uca.sisbec.commons.SisbecAction;
import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.forms.LoginForm;
import com.uca.sisbec.forms.usuario.ChangePassForm;
import com.uca.sisbec.helpers.LoginHelper;
import com.uca.sisbec.helpers.manto.UsuarioHelper;
import com.uca.sisbec.mapping.Usuario;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

public class LoginAction extends SisbecAction {
    public ActionForward init (ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) {
        Random r = new Random();
        request.getSession().invalidate();
        request.setAttribute("imgRdm", r.nextInt(4) + 1);
        return mapping.findForward("success");
    }
    
    public ActionForward loginUser (ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) {
        LoginForm lf = (LoginForm) form;
        LoginHelper helper = new LoginHelper(getSession(request));
        ActionErrors errors = new ActionErrors();
        Usuario user = helper.getUser(lf);
        if (user != null) {
            if (user.getContrasenia().equals(Utils.encriptarMD5(lf.getPass()))) {
                request.getSession().setAttribute(_USER, user);
                if (user.getChgPass().equals("Y"))
                    return mapping.findForward("chgPass");
                String menuPrincipal = helper.getMenuXRol(user.getRol().getIdRol());
                String menuBecarios = helper.getMenuXRolBecario(user.getRol().getIdRol());
                request.getSession().setAttribute(_MENU, menuPrincipal);
                request.getSession().setAttribute(_MENU_BECARIOS, menuBecarios);
            } else {
                // Password invalido
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.lbl.pass.error"));
                request.setAttribute("errors", errors);
            }
        } else {
            // Usuario no existe
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.lbl.user.error"));
            request.setAttribute("errors", errors);
        }
        if (!errors.isEmpty()){
            lf.setUser("");
            lf.setPass("");
            Random r = new Random();
            request.setAttribute("imgRdm", r.nextInt(4) + 1);
            return mapping.findForward("onlyPersonal");
        }
        return mapping.findForward("enter");
    }
    
    public ActionForward logoffUser (ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return mapping.findForward("exit");
    }
    
    public ActionForward initChgPass(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ChangePassForm cpf = (ChangePassForm) form;
        cpf.setUser(((Usuario)request.getSession().getAttribute(_USER)).getAlias());
        return mapping.findForward("init");
    }
    
    public ActionForward changePassword(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ChangePassForm cpf = (ChangePassForm) form;
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        ActionErrors error  = new ActionErrors();
        Usuario user = helper.buscarUsuario(cpf.getUser());
        validate(user, cpf, error);
        if (!error.isEmpty()){
            request.setAttribute("errors", error);
            return mapping.findForward("invalidPass");
        }
        helper.changePassword(cpf.getPassNew(), user);
        //request.getSession().setAttribute(_USER, user);
        LoginForm lf = new LoginForm();
        lf.setUser(cpf.getUser());
        lf.setPass(cpf.getPassNew());
        //return mapping.findForward("success");
        return loginUser(mapping, lf, request, response);
    }

    private void validate(Usuario user, ChangePassForm cpf, ActionErrors error) {
        if (!Utils.encriptarMD5(cpf.getPassOld()).equals(user.getContrasenia())){
            error.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.error.chgPass"));
        } else if (Utils.encriptarMD5(cpf.getPassNew()).equals(user.getContrasenia())){
            error.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.error.chgPass3"));
        } else if (!cpf.getPassNew().equals(cpf.getPassCon())){
                error.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.error.chgPass2"));
        }
    }
}
